package sg.edu.nus.iss.stockex.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;

import sg.edu.nus.iss.JOB;
import sg.edu.nus.iss.Util;
import sg.edu.nus.iss.stockex.LinearRegressionWithPoints;
import sg.edu.nus.iss.stockex.MovingAverage;
import sg.edu.nus.iss.stockex.RelativeStrengthIndex;
import sg.edu.nus.iss.stockex.StochasticOscillator;
import sg.edu.nus.iss.stockex.StockCode;
/**
 * Servlet implementation class StockAnalysisServlet
 */
public class StockAnalysisServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AmazonEC2      ec2;  
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StockAnalysisServlet() {
        super();
        
        if (ec2 == null) {
            AWSCredentials credentials = null;
			try {
				credentials = new PropertiesCredentials(
				    getClass().getClassLoader().getResourceAsStream("AwsCredentials.properties"));
			} catch (IOException e) {
				e.printStackTrace();
			}
            ec2 = new AmazonEC2Client(credentials);
            ec2.describeInstances().getReservations();
        }
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		JOB job = new JOB();
		List<StockCode> stockCodeList = job.getAllStockCodes();
		for (StockCode code : stockCodeList)
		System.out.println("code = "+ code.getCode());
		request.getSession().setAttribute("stockCodeList", stockCodeList);
		getServletContext().getRequestDispatcher("/stockAnalysis.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JOB job = new JOB();	
		
		String stockCode = request.getParameter("code");
		//String stockCode = stockNameCode.substring(stockNameCode.indexOf('[')+1, stockNameCode.indexOf(']'));
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int date = Integer.parseInt(request.getParameter("date"));
		boolean isValidDate = Util.isDateValid(year, month, date);
		
		if (!isValidDate) return;
		
		System.out.println("Code : " + stockCode);
		System.out.println("yyyy-MM-dd : " + year + "-" + month + "-" +  date);
		Calendar cal = Calendar.getInstance();
		cal.set(year, month-1, date);
		
		LinearRegressionWithPoints linearRegressionWithPoints = job.getLinearRegression(stockCode, cal.getTime());
		
		List<StochasticOscillator> stochasticOscillatorList =  job.getScholasticOscillator(stockCode, cal.getTime());
		System.out.println("stochasticOscillatorList.size() = " + stochasticOscillatorList.size());
		
		List<RelativeStrengthIndex> relativeStrengthIndexList = job.getRelativeStrengthIndex(stockCode, cal.getTime());
		System.out.println("relativeStrengthIndexList.size() = " + relativeStrengthIndexList.size());
		
		List<MovingAverage> movingAverageList = job.getMovingAverage(stockCode, cal.getTime());
		System.out.println("movingAverageList.size() = " + movingAverageList.size());
		
		request.getSession().setAttribute("linearRegressionWithPoints", linearRegressionWithPoints);
		request.getSession().setAttribute("movingAverageList", movingAverageList);
		request.getSession().setAttribute("stochasticOscillatorList", stochasticOscillatorList);
		request.getSession().setAttribute("relativeStrengthIndexList", relativeStrengthIndexList);
		
		getServletContext().getRequestDispatcher("/stockAnalysis.jsp").forward(request, response);
	
	}

}
